function getId(goodsId) {
  let id = window.location.search.substr(1).match(new RegExp("(^|&)" + goodsId + "=([^&]*)(&|$)"))
  return id != null? unescape(id[2]):null
}
let goodsId = getId("goodsId");

document.querySelector('.nav_a').click()
document.querySelector('.icon-arrow-left').addEventListener('click',function () {
  history.back()
})

axios.get('/comment/list',{
  params: {
    scoreType:-1,
    goodsId,
    page:1
  }
}).then(({ data: res }) => {
  render(res.data.list.data)
})

//tap栏切换
document.querySelector('.nav').addEventListener('click',function (e) {
  if (e.target.tagName === 'DIV') { 
    const active= document.querySelector('.nav .active')
    if (active) { 
      active.classList.remove('active')
    }
    e.target.classList.add('active')
    //数据渲染
axios.get('/comment/list', {
  params: {
    scoreType:e.target.dataset.id,
    goodsId,
    page:1
  }
}).then(({ data: res }) => {
      render(res.data.list.data)
    })
  }
})

function render(arr){ 
  let str = ''
  let str1 = ''
  arr.forEach(item => {
    if (item.images!==[]) { 
      item.images.forEach(item => { 
            str1+=` <img
            src=${item.image_url}
            alt=""
          />`
      })
      console.log(str1)
    }
    item.user.avatar_url = item.user.avatar_url ? item.user.avatar_url : 'http://cba.itlike.com/public/mweb/static/default-avatar.png'
    
    str+=`<div class="box_list">
    <div class="box_list_top">
      <img
        src=${item.user.avatar_url}
        alt=""
      />
      <span class="name">${item.user.nick_name}</span>
      <span class="xin">❤❤❤❤❤</span>
      <span class="time">${item.create_time}</span>
    </div>
    <div class="title">${item.content}</div>
    <div class="box_list_img">
    ${str1}
    </div>
  </div>`
        })
      document.querySelector('.box').innerHTML=str
}



// 渲染评论个数
 axios.get('/comment/list', {
    params: {
      scoreType: -1,
      goodsId,
      page: 1
    }
 }).then((res) => { 
  document.querySelector('.nav_a span').innerHTML = `(${res.data.data.list.total})`
 })

 axios.get('/comment/list', {
  params: {
    scoreType: 10,
    goodsId,
    page: 1
  }
}).then((res) => { 
document.querySelector('.nav_b span').innerHTML = `(${res.data.data.list.total})`
})
axios.get('/comment/list', {
  params: {
    scoreType: 20,
    goodsId,
    page: 1
  }
}).then((res) => { 
document.querySelector('.nav_c span').innerHTML = `(${res.data.data.list.total})`
})
axios.get('/comment/list', {
  params: {
    scoreType: 30,
    goodsId,
    page: 1
  }
}).then((res) => { 
document.querySelector('.nav_d span').innerHTML = `(${res.data.data.list.total})`
})


